package com.example.service.impl;

import com.example.DTO.DataSetDTO;
import com.example.DTO.DataSetPageQueryDTO;
import com.example.Exception.SourceException;
import com.example.Exception.StatusException;
import com.example.VO.DataSetVO;
import com.example.constant.MessageConstant;
import com.example.entity.DataSet;
import com.example.mapper.DataSetMapper;
import com.example.result.PageResult;
import com.example.service.DataSetService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@Slf4j
public class DataSetServiceImpl implements DataSetService {

    @Autowired
    private DataSetMapper dataSetMapper;

    /**
     * 新增数据集
     * @param dataSetDTO
     */
    @Transactional
    public void save(DataSetDTO dataSetDTO) {
        DataSet dataSet = new DataSet();
        BeanUtils.copyProperties(dataSetDTO,dataSet);
        // 根据传入的 datasetName 进行数据库查询
        DataSet existingDataSet = dataSetMapper.findByDatasetName(dataSet.getDatasetName());
        if (existingDataSet != null) {
            // 如果查询到了相同名称的数据集，则抛出异常
            throw new StatusException(MessageConstant.ALREADY_EXISTS);
        }
        LocalDateTime currentTime = LocalDateTime.now();
        dataSet.setCreateTime(currentTime);
        dataSet.setUpdateTime(currentTime);
        dataSet.setDatasetSource(0);
        dataSet.setDatasetStatus(1);
        dataSetMapper.insert(dataSet);
    }
    /**
     * 数据集分页查询
     *
     * @param dataSetPageQueryDTO
     * @return
     */
    public PageResult pageQuery(DataSetPageQueryDTO dataSetPageQueryDTO) {
        int pageNum = dataSetPageQueryDTO.getPage();
        int pageSize = dataSetPageQueryDTO.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        Page<DataSetVO> page = dataSetMapper.pageQuery(dataSetPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 删除数据集
     * @param id
     */
    @Transactional
    public void deleteById(Long id) {
        DataSet dataSet = dataSetMapper.getById(id);
        if (dataSet.getDatasetSource() == 1){
            throw new SourceException(MessageConstant.PROTECT_BY_SYSTEMS);
        }
        if (dataSet.getDatasetStatus() == 1){
            throw new StatusException(MessageConstant.STATUS_ERROR);
        }
        //若不再使用，则删除
        dataSetMapper.deleteById(id);
    }
    /**
     * 根据id查询数据集，用于修改页面回显数据
     *
     * @param id
     * @return
     */
    @Override
    public DataSetVO getById(Long id) {
        return dataSetMapper.getByIdVO(id);
    }

    /**
     * 修改数据集
     * @param dataSetDTO
     */
    @Transactional
    public void update(DataSetDTO dataSetDTO) {
        DataSet dataSet = new DataSet();
        BeanUtils.copyProperties(dataSetDTO,dataSet);
        if (dataSet.getDatasetSource().equals(1)){
            throw new StatusException(MessageConstant.CALL_TO_BOSS);
        }
        dataSet.setUpdateTime(LocalDateTime.now());
        dataSetMapper.update(dataSet);
    }
}